package com.codeerror.demo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.codeerror.demo.R;
import com.codeerror.demo.bean.WordsBean;

import org.raphets.roundimageview.RoundImageView;

import java.util.List;

public class WordsListRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int NORMAL_WORDS = 0; // 正常的显示句子
    public static final int ADD_WORDS = 1; // 添加句子

    private final List<WordsBean> wordsList;
    private OnClickWordsListener onClickWordsListener;

    public WordsListRecyclerAdapter(List<WordsBean> wordsList) {
        this.wordsList = wordsList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == NORMAL_WORDS){
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_recycler_normal_words, parent, false);
            return new WordsViewHolder(view) {};
        }else if (viewType == ADD_WORDS){
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_recycler_add_words, parent, false);
            return new RecyclerView.ViewHolder(view) {};
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == NORMAL_WORDS){
            WordsBean bean = wordsList.get(position);
            WordsViewHolder wordsHolder = (WordsViewHolder) holder;
            wordsHolder.riv.setImageResource(bean.getBgId());
            wordsHolder.words.setText(bean.getWords());
        }
        holder.itemView.setOnClickListener(view ->
                onClickWordsListener.onClickWords(position, getItemViewType(position)));
    }

    @Override
    public int getItemViewType(int position) {
        if (position == wordsList.size()){
            return ADD_WORDS;
        }else {
            return NORMAL_WORDS;
        }
    }

    @Override
    public int getItemCount() {
        return wordsList.size() + 1;
    }

    public void setOnClickWordsListener(OnClickWordsListener onClickWordsListener) {
        this.onClickWordsListener = onClickWordsListener;
    }

    public interface OnClickWordsListener{
        /**
         * 每日一句点击事件
         * @param type 点击的类型
         */
        void onClickWords(int position, int type);
    }

    static class WordsViewHolder extends RecyclerView.ViewHolder{
        RoundImageView riv;
        TextView words;

        public WordsViewHolder(@NonNull View itemView) {
            super(itemView);
            riv = itemView.findViewById(R.id.riv_give_yourself_words);
            words = itemView.findViewById(R.id.tv_give_yourself_words);
        }
    }
}
